package com.bl.student.service.biz.order.impl;

import com.bl.student.common.ApiResponseCode;
import com.bl.student.common.CommonServiceException;
import com.bl.student.common.enums.SearchType;
import com.bl.student.common.utils.UUIDUtils;
import com.bl.student.dao.model.*;
import com.bl.student.dao.repository.*;
//import com.bl.student.service.biz.address.AddressService;
import com.bl.student.service.biz.address.AddressService;
import com.bl.student.service.biz.goods.GoodsService;
//import com.bl.student.service.biz.money.MoneyService;
import com.bl.student.service.biz.order.OrderService;
import com.bl.student.service.biz.user.UserService;
//import com.bl.student.service.vo.DelBody;
//import com.bl.student.service.vo.DelListBody;
//import com.bl.student.service.vo.address.AddressVo;
//import com.bl.student.service.vo.goods.GoodsBuyVo;
//import com.bl.student.service.vo.goods.GoodsVo;
//import com.bl.student.service.vo.ico.IcoAddVo;
//import com.bl.student.service.vo.ico.IcoListVo;
//import com.bl.student.service.vo.ico.IcoModVo;
import com.bl.student.service.vo.address.AddressVo;
import com.bl.student.service.vo.goods.GoodsBuyVo;
import com.bl.student.service.vo.order.OrderListVo;
//import com.bl.student.service.vo.order.OrderModVo;
import com.bl.student.service.vo.order.OrderModVo;
import com.bl.student.service.vo.order.OrderVo;
import com.bl.student.service.vo.order.OrderVoListVo;
//import com.bl.student.service.vo.withdrawals.WithdrawalsListVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liangweikang on 18/7/20.
 */
@Service
@Log4j2
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderRepository orderRepository;

    @Autowired
    GoodsService goodsService;

    @Autowired
    UserService userService;

    @Autowired
    UserRepository userRepository;

    @Autowired
    AddressService addressService;


    @Override
    public String add(GoodsEntity goodsEntity, String userId, String userName, String mobile, int num) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setCreateTime(new Date());
        orderEntity.setUpdateTime(new Date());
        orderEntity.setId(UUIDUtils.getUUID());
        orderEntity.setExpress("");
        orderEntity.setGoodsName(goodsEntity.getTitle());
        orderEntity.setGoodsId(goodsEntity.getId());

        orderEntity.setPartybId(userId);
        orderEntity.setPartybName(userName);
        orderEntity.setPartybMobile(mobile);
        // TODO sn
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String sn = simpleDateFormat.format(date) + orderEntity.getId().substring(0,3);
        orderEntity.setOrdersn(sn);

        orderEntity.setPoint(goodsEntity.getPoint() * num);
        orderEntity.setNum(num);


        AddressVo addressVo = addressService.getAddressVo(userId);
        orderEntity.setAddress(addressVo.getPartybAddress());

        orderRepository.save(orderEntity);

        return orderEntity.getId();
    }

    @Override
    public OrderListVo getOrderListsMulti(String partybId, String goodsId, String ordersn , String express, String status, Pageable pageable ){
        Page <OrderEntity> orderEntities;
        OrderListVo orderListVo = new OrderListVo();

        if(partybId == null && goodsId == null && ordersn == null && express == null&& status == null) {
            orderEntities = orderRepository.findAll(pageable );
        } else {
            orderEntities = orderRepository.findMultList(partybId, goodsId, ordersn, express, status, pageable );
        }
        orderListVo.setTotalCount(orderEntities.getTotalElements());
        orderListVo.setList(orderEntities.getContent());

        return orderListVo;
    }

    @Override
    public OrderVoListVo getListByUser(Pageable pageable, String userId, String status) {
        OrderVoListVo orderVoListVo = new OrderVoListVo();
        List<OrderVo> orderListVo = new ArrayList<>();

        Page<OrderEntity> orderEntities;

        orderEntities = orderRepository.findByPartybIdAndStatusOrderByCreateTimeDesc(userId,status, pageable);
        for (OrderEntity orderEntity : orderEntities) {
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(orderEntity, orderVo);
            if (orderEntity.getGoodsId() != null) {
                GoodsEntity goodsVo = goodsService.getGoodDetail(orderEntity.getGoodsId());
                orderVo.setImage(goodsVo.getImage());
            }
            orderVo.setGoodsName(orderEntity.getGoodsName());
            orderVo.setStatus(orderEntity.getStatus());
            orderVo.setPoint(orderEntity.getPoint());
            orderListVo.add(orderVo);
        }

        orderVoListVo.setTotalCount(orderEntities.getTotalElements());
        orderVoListVo.setList(orderListVo);

        return orderVoListVo;
    }



    @Override
    public void mod(OrderModVo orderModVo) {
        OrderEntity orderEntity = orderRepository.findOne(orderModVo.getId());
        BeanUtils.copyProperties(orderModVo, orderEntity);
        orderEntity.setUpdateTime(new Date());
        orderRepository.save(orderEntity);
    }


}
